﻿#region Documentation
/*
 * Definition for the PaymentViewModel used to query for a specific result set.
 *  
 * Copyright (C) 2014  Daniel Hamacher
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * 
 * */
#endregion

using SpendingMonitor.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpendingMonitor.ViewModels
{
    class PaymentsViewModel : ViewModelBase
    {        
        private ObservableCollection<PaymentViewModel> payments;
        public ObservableCollection<PaymentViewModel> Payments
        {
            get
            {
                return payments;
            }

            set
            {
                payments = value;
                RaisePropertyChanged("Payments");
            }
        }

        public ObservableCollection<PaymentViewModel> GetPayments()
        {
            payments = new ObservableCollection<PaymentViewModel>();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var query = db.Table<Payment>().OrderBy(c => c.Id);
                foreach (var _payments in query)
                {
                    var entry = new PaymentViewModel()
                    {
                        Id = _payments.Id,
                        Category = _payments.Category,
                        Retailer = _payments.Retailer,
                        PaymentMethod = _payments.PaymentMethod,
                        Price = _payments.Price,
                        PayedOn = _payments.PayedOn
                    };
                    payments.Add(entry);
                }
            }
            return payments;
        }

        public ObservableCollection<PaymentViewModel> GetPaymentsByPaymentMethod(string paymentMethod)
        {
            payments = new ObservableCollection<PaymentViewModel>();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var query = db.Table<Payment>().ToList().Where(x => x.PaymentMethod.Equals(paymentMethod)).OrderBy(x => x.PayedOn);
                foreach (var _payments in query)
                {
                    var entry = new PaymentViewModel()
                    {
                        Id = _payments.Id,
                        Category = _payments.Category,
                        Retailer = _payments.Retailer,
                        PaymentMethod = _payments.PaymentMethod,
                        Price = _payments.Price,
                        PayedOn = _payments.PayedOn
                    };
                    payments.Add(entry);
                }
            }
            return payments;
        }

        public ObservableCollection<PaymentViewModel> GetPaymentsByCategory(string category)
        {
            payments = new ObservableCollection<PaymentViewModel>();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var query = db.Table<Payment>().ToList().Where(x => x.Category.Equals(category)).OrderBy(x => x.PayedOn);
                foreach (var _payments in query)
                {
                    var entry = new PaymentViewModel()
                    {
                        Id = _payments.Id,
                        Category = _payments.Category,
                        Retailer = _payments.Retailer,
                        PaymentMethod = _payments.PaymentMethod,
                        Price = _payments.Price,
                        PayedOn = _payments.PayedOn
                    };
                    payments.Add(entry);
                }
            }
            return payments;
        }

        public ObservableCollection<PaymentViewModel> GetPaymentsByRetailer(string retailer)
        {
            payments = new ObservableCollection<PaymentViewModel>();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var query = db.Table<Payment>().ToList().Where(x => x.Retailer.Equals(retailer)).OrderBy(x => x.PayedOn);
                foreach (var _payments in query)
                {
                    var entry = new PaymentViewModel()
                    {
                        Id = _payments.Id,
                        Category = _payments.Category,
                        Retailer = _payments.Retailer,
                        PaymentMethod = _payments.PaymentMethod,
                        Price = _payments.Price,
                        PayedOn = _payments.PayedOn
                    };
                    payments.Add(entry);
                }
            }
            return payments;
        }    
    }
}
